home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / Delphi.Net / CLR / WMIdemo / WMIDemo.dpr < prev   
Encoding:
Text File  |  2004-10-22  |  18.4 KB  |  548 lines

  1. program WMIDemo;
  2. //------------------------------------------------------------------------------
  3. //  File name:      WMIDemo.dpr
  4. //  Last updated:   11/16/03
  5. //  Author:         Sergey Mishkovskiy
  6. //  Company:        USysWare, Inc.
  7. //  Contact info:   usysware@comcast.net
  8. //
  9. //  Compatibility:  Borland Delphi for .NET
  10. //
  11. //  Description:    WMI Demo application.
  12. //------------------------------------------------------------------------------
  13.  
  14. {$APPTYPE CONSOLE}
  15.  
  16. {%DelphiDotNetAssemblyCompiler '$(SystemRoot)\microsoft.net\framework\v1.1.4322\System.dll'}
  17. {%DelphiDotNetAssemblyCompiler '$(SystemRoot)\microsoft.net\framework\v1.1.4322\System.Management.dll'}
  18.  
  19. uses
  20.   System.Management;
  21.  
  22. type
  23.   TStringsArray = array of string;
  24.  
  25. var
  26.   Scope: ManagementScope;
  27.  
  28. function GetManagementCollection(Scope: ManagementScope;
  29.   const WMIClassName: string): ManagementObjectCollection;
  30. var
  31.   Query: ObjectQuery;
  32.   Searcher: ManagementObjectSearcher;
  33. begin
  34.   // Query the WMI
  35.   Query := ObjectQuery.Create('select * from ' + WMIClassName);
  36.   Searcher := ManagementObjectSearcher.Create(Scope, Query);
  37.   Result := Searcher.Get;
  38. end;
  39.  
  40. function GetManagementCollectionEx(Scope: ManagementScope;
  41.   const WMIClassName, WhereClause: string): ManagementObjectCollection;
  42. var
  43.   Query: ObjectQuery;
  44.   Searcher: ManagementObjectSearcher;
  45. begin
  46.   // Query the WMI
  47.   Query := ObjectQuery.Create('select * from ' + WMIClassName +
  48.     ' where ' + WhereClause);
  49.   Searcher := ManagementObjectSearcher.Create(Scope, Query);
  50.   Result := Searcher.Get;
  51. end;
  52.  
  53. function GMTToLocalDateTime(const DT: string): DateTime;
  54. var
  55.   GMT: TimeSpan;
  56. begin
  57.   GMT := TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);
  58.  
  59.   Result := DateTime.Parse(
  60.     DT.Substring(4, 2) + '/' +
  61.     DT.Substring(6, 2) + '/' +
  62.     DT.Substring(0, 4) + ' ' +
  63.     DT.Substring(8, 2) + ':' +
  64.     DT.Substring(10,2) + ':' +
  65.     DT.Substring(12,2)).Subtract(GMT);
  66. end;
  67.  
  68. function GMTToLocalDateTimeStr(const DT: string): string;
  69. begin
  70.   Result := GMTToLocalDateTime(DT).ToString;
  71. end;
  72.  
  73. procedure WMIGetOSInfo(Scope: ManagementScope);
  74. var
  75.   QueryCollection: ManagementObjectCollection;
  76.   Enumerator: ManagementObjectCollection.ManagementObjectEnumerator;
  77.   Item: ManagementObject;
  78.   UpTime: TimeSpan;
  79.   TotalRam, FreeRam, UsedRam: System.Double;
  80. begin
  81.   QueryCollection := GetManagementCollection(Scope, 'Win32_OperatingSystem');
  82.   Enumerator := QueryCollection.GetEnumerator;
  83.  
  84.   Console.WriteLine;
  85.   Console.WriteLine('OS Information');
  86.   Console.WriteLine('--------------');
  87.   while Enumerator.MoveNext do
  88.   begin
  89.     Item := Enumerator.Current as ManagementObject;
  90.     with Item, Console do
  91.     begin
  92.       UpTime := DateTime.Now.Subtract(
  93.         GMTToLocalDateTime(GetPropertyValue('LastBootupTime').ToString));
  94.  
  95.       TotalRam := System.Double.Parse(
  96.         GetPropertyValue('TotalVisibleMemorySize').ToString)/1024;
  97.       FreeRam := System.Double.Parse(
  98.         GetPropertyValue('FreePhysicalMemory').ToString)/1024;
  99.       UsedRam := TotalRam - FreeRam;
  100.  
  101.       WriteLine('Name: ' + GetPropertyValue('Caption').ToString);
  102.       WriteLine('Version: ' + GetPropertyValue('Version').ToString);
  103.       WriteLine('Build: ' + GetPropertyValue('BuildNumber').ToString);
  104.       if UInt16.Parse(
  105.          GetPropertyValue('ServicePackMajorVersion').ToString) <> 0 then
  106.         WriteLine('Service Pack: ' +
  107.         GetPropertyValue('ServicePackMajorVersion').ToString);
  108.       WriteLine('Installed Date: ' +
  109.         GMTToLocalDateTimeStr(GetPropertyValue('InstallDate').ToString));
  110.       WriteLine('Last Rebooted: ' + 
  111.         GMTToLocalDateTimeStr(GetPropertyValue('LastBootupTime').ToString));
  112.       WriteLine('Up Time: ' + UpTime.Days.ToString + ' days, ' +
  113.         UpTime.Hours.ToString + ' hours, ' +
  114.         UpTime.Minutes.ToString + ' minutes');
  115.       WriteLine('Windows Directory: ' +
  116.         GetPropertyValue('WindowsDirectory').ToString);
  117.       WriteLine('System Directory: ' +
  118.         GetPropertyValue('SystemDirectory').ToString);
  119.       WriteLine('RAM Total: ' + Math.Ceiling(TotalRam).ToString + ' MB');
  120.       WriteLine('RAM Used: ' + Math.Ceiling(UsedRam).ToString + ' MB');
  121.       WriteLine('Computer Name: ' + GetPropertyValue('CSName').ToString);
  122.       WriteLine('Number Of Processes: ' +
  123.         GetPropertyValue('NumberOfProcesses').ToString);
  124.     end;
  125.   end;
  126. end;
  127.  
  128. procedure WMIGetComputerInfo(Scope: ManagementScope);
  129. var
  130.   QueryCollection: ManagementObjectCollection;
  131.   Enumerator: ManagementObjectCollection.ManagementObjectEnumerator;
  132.   Item: ManagementObject;
  133. begin
  134.   QueryCollection := GetManagementCollection(Scope, 'Win32_ComputerSystem');
  135.   Enumerator := QueryCollection.GetEnumerator;
  136.  
  137.   Console.WriteLine;
  138.   Console.WriteLine('Computer Information');
  139.   Console.WriteLine('--------------------');
  140.   while Enumerator.MoveNext do
  141.   begin
  142.     Item := Enumerator.Current as ManagementObject;
  143.     with Item, Console do
  144.     begin
  145.       WriteLine('Computer Type: ' + GetPropertyValue('SystemType').ToString);
  146.       WriteLine('Manufacturer: ' + GetPropertyValue('Manufacturer').ToString);
  147.       WriteLine('Model: ' + GetPropertyValue('Model').ToString);
  148.       WriteLine('Number Of Processors: ' +
  149.         GetPropertyValue('NumberOfProcessors').ToString);
  150.       WriteLine('Domain: ' + GetPropertyValue('Domain').ToString);
  151.       WriteLine('Logged On User: ' + GetPropertyValue('UserName').ToString);
  152.     end;
  153.   end;
  154. end;
  155.  
  156. procedure WMIGetBIOSInfo(Scope: ManagementScope);
  157. var
  158.   QueryCollection: ManagementObjectCollection;
  159.   Enumerator: ManagementObjectCollection.ManagementObjectEnumerator;
  160.   Item: ManagementObject;
  161. begin
  162.   QueryCollection := GetManagementCollection(Scope, 'Win32_BIOS');
  163.   Enumerator := QueryCollection.GetEnumerator;
  164.  
  165.   Console.WriteLine;
  166.   Console.WriteLine('BIOS Information');
  167.   Console.WriteLine('----------------');
  168.   while Enumerator.MoveNext do
  169.   begin
  170.     Item := Enumerator.Current as ManagementObject;
  171.     with Item, Console do
  172.     begin
  173.       WriteLine('BIOS: ' + GetPropertyValue('Version').ToString);
  174.       WriteLine('Manufacturer: ' + GetPropertyValue('Manufacturer').ToString);
  175.       WriteLine('Release Date: ' +
  176.         GMTToLocalDateTimeStr(GetPropertyValue('ReleaseDate').ToString));
  177.       WriteLine('Serial Number: ' + GetPropertyValue('SerialNumber').ToString);
  178.     end;
  179.   end;
  180. end;
  181.  
  182. procedure WMIGetCDROMInfo(Scope: ManagementScope);
  183. var
  184.   QueryCollection: ManagementObjectCollection;
  185.   Enumerator: ManagementObjectCollection.ManagementObjectEnumerator;
  186.   Item: ManagementObject;
  187. begin
  188.   QueryCollection := GetManagementCollection(Scope, 'Win32_CDROMDrive');
  189.   Enumerator := QueryCollection.GetEnumerator;
  190.  
  191.   Console.WriteLine;
  192.   Console.WriteLine('CD-ROM Information');
  193.   Console.WriteLine('------------------');
  194.   while Enumerator.MoveNext do
  195.   begin
  196.     Item := Enumerator.Current as ManagementObject;
  197.     with Item, Console do
  198.     begin
  199.       WriteLine('Name: ' + GetPropertyValue('Caption').ToString);
  200.       WriteLine('Description: ' + GetPropertyValue('Description').ToString);
  201.       WriteLine('Drive: ' + GetPropertyValue('Drive').ToString);
  202.     end;
  203.   end;
  204. end;
  205.  
  206. procedure WMIGetVideoInfo(Scope: ManagementScope);
  207. var
  208.   WinXPandUp: Boolean;
  209.   QueryCollection: ManagementObjectCollection;
  210.   Enumerator: ManagementObjectCollection.ManagementObjectEnumerator;
  211.   Item: ManagementObject;
  212.   Value: System.Double; 
  213. begin
  214.   WinXPandUp := ((Environment.OSVersion.Version.Major > 5) or
  215.     ((Environment.OSVersion.Version.Major = 5) and
  216.      (Environment.OSVersion.Version.Minor >= 1)));
  217.  
  218.   if WinXPandUp then
  219.     QueryCollection := GetManagementCollection(
  220.       Scope, 'Win32_VideoController')
  221.   else
  222.     QueryCollection := GetManagementCollection(
  223.       Scope, 'Win32_VideoConfiguration');
  224.   Enumerator := QueryCollection.GetEnumerator;
  225.  
  226.   Console.WriteLine;
  227.   Console.WriteLine('Video Information');
  228.   Console.WriteLine('-----------------');
  229.   while Enumerator.MoveNext do
  230.   begin
  231.     Item := Enumerator.Current as ManagementObject;
  232.     with Item, Console do
  233.     begin
  234.       if WinXPandUp then
  235.       begin
  236.         WriteLine('Card: ' + GetPropertyValue('AdapterCompatibility').ToString);
  237.         WriteLine('Type: ' + GetPropertyValue('VideoProcessor').ToString);
  238.       end
  239.       else
  240.       begin
  241.         WriteLine('Card: ' + GetPropertyValue('AdapterChipType').ToString);
  242.         WriteLine('Type: ' + GetPropertyValue('AdapterType').ToString);
  243.       end;
  244.  
  245.       Value := System.Double.Parse(
  246.         GetPropertyValue('AdapterRAM').ToString)/1024/1024;
  247.       WriteLine('RAM: ' + Value.ToString + ' MB');
  248.     end;
  249.   end;
  250. end;
  251.  
  252. procedure WMIGetRegistryInfo(Scope: ManagementScope);
  253. var
  254.   QueryCollection: ManagementObjectCollection;
  255.   Enumerator: ManagementObjectCollection.ManagementObjectEnumerator;
  256.   Item: ManagementObject;
  257. begin
  258.   QueryCollection := GetManagementCollection(Scope, 'Win32_Registry');
  259.   Enumerator := QueryCollection.GetEnumerator;
  260.  
  261.   Console.WriteLine;
  262.   Console.WriteLine('Registry Information');
  263.   Console.WriteLine('--------------------');
  264.   while Enumerator.MoveNext do
  265.   begin
  266.     Item := Enumerator.Current as ManagementObject;
  267.     with Item, Console do
  268.     begin
  269.       WriteLine('Current Size: ' + GetPropertyValue('CurrentSize').ToString +
  270.         ' MB');
  271.       WriteLine('Maximum Size: ' + GetPropertyValue('MaximumSize').ToString +
  272.         ' MB');
  273.     end;
  274.   end;
  275. end;
  276.  
  277. procedure WMIGetDrivesInfo(Scope: ManagementScope);
  278. var
  279.   QueryCollection: ManagementObjectCollection;
  280.   Enumerator: ManagementObjectCollection.ManagementObjectEnumerator;
  281.   Item: ManagementObject;
  282.   TotalRam, FreeRam, UsedRam: System.Double;
  283. begin
  284.   QueryCollection := GetManagementCollectionEx(Scope, 'Win32_LogicalDisk',
  285.     'DriveType<>2 and DriveType<>5');
  286.   Enumerator := QueryCollection.GetEnumerator;
  287.  
  288.   Console.WriteLine;
  289.   Console.WriteLine('Non-Removable Drives Information');
  290.   Console.WriteLine('--------------------------------');
  291.   while Enumerator.MoveNext do
  292.   begin
  293.     Item := Enumerator.Current as ManagementObject;
  294.     with Item, Console do
  295.       case uint32.Parse(GetPropertyValue('DriveType').ToString) of
  296.         3:
  297.           begin
  298.             TotalRam := Math.Round(System.Double.Parse(
  299.               GetPropertyValue('Size').ToString)/1024/1024/1024,1);
  300.             FreeRam := Math.Round(System.Double.Parse(
  301.               GetPropertyValue('FreeSpace').ToString)/1024/1024/1024,1);
  302.             UsedRam := TotalRam - FreeRam;
  303.  
  304.             WriteLine(GetPropertyValue('DeviceID').ToString + ' Used: ' +
  305.               UsedRam.ToString + ' GB Total: ' + TotalRam.ToString + ' GB');
  306.           end;
  307.         4: WriteLine(GetPropertyValue('DeviceID').ToString + ' ' +
  308.           GetPropertyValue('ProviderName').ToString);
  309.       else
  310.         WriteLine(GetPropertyValue('DeviceID').ToString + ' (' +
  311.           GetPropertyValue('Description').ToString + ')');
  312.       end;
  313.   end;
  314. end;
  315.  
  316. procedure WMIGetNetConfigInfo(Scope: ManagementScope);
  317. var
  318.   QueryCollection: ManagementObjectCollection;
  319.   Enumerator: ManagementObjectCollection.ManagementObjectEnumerator;
  320.   Item: ManagementObject;
  321.   Items: TStringsArray;
  322.   Index: Integer;
  323. begin
  324.   QueryCollection := GetManagementCollection(Scope,
  325.     'Win32_NetworkAdapterConfiguration');
  326.   Enumerator := QueryCollection.GetEnumerator;
  327.  
  328.   Console.WriteLine;
  329.   Console.WriteLine('Network Configuration Information');
  330.   Console.WriteLine('---------------------------------');
  331.   while Enumerator.MoveNext do
  332.   begin
  333.     Item := Enumerator.Current as ManagementObject;
  334.     with Item, Console do
  335.       if Boolean(GetPropertyValue('IPEnabled')) then
  336.       begin
  337.         WriteLine('NIC: ' + GetPropertyValue('description').ToString);
  338.         WriteLine('MAC Address: ' + GetPropertyValue('MACAddress').ToString);
  339.         if GetPropertyValue('DHCPServer') <> nil then
  340.           WriteLine('DHCP Server: ' + GetPropertyValue('DHCPServer').ToString)
  341.         else
  342.           WriteLine('DHCP Server: ');
  343.         WriteLine('DHCP Enabled: ' + GetPropertyValue('DHCPenabled').ToString);
  344.         if GetPropertyValue('DNSDomain') <> nil then
  345.           WriteLine('DNS Domain: ' + GetPropertyValue('DNSDomain').ToString)
  346.         else
  347.           WriteLine('DNS Domain: ');
  348.         WriteLine('DNS Host: ' + GetPropertyValue('DNSHostName').ToString);
  349.  
  350.         Items := TStringsArray(GetPropertyValue('IPAddress'));
  351.         if Items <> nil then
  352.         begin
  353.           WriteLine('IP Addresses:');
  354.           for Index := 0 to Length(Items) - 1 do
  355.             WriteLine('  ' + Items[Index]);
  356.         end;
  357.  
  358.         Items := TStringsArray(GetPropertyValue('IPSubnet'));
  359.         if Items <> nil then
  360.         begin
  361.           WriteLine('Subnet Masks:');
  362.           for Index := 0 to Length(Items) - 1 do
  363.             WriteLine('  ' + Items[Index]);
  364.         end;
  365.  
  366.         Items := TStringsArray(GetPropertyValue('DefaultIPGateway'));
  367.         if Items <> nil then
  368.         begin
  369.           WriteLine('Default Gateways:');
  370.           for Index := 0 to Length(Items) - 1 do
  371.             WriteLine('  ' + Items[Index]);
  372.         end;
  373.  
  374.         Items := TStringsArray(GetPropertyValue('DNSServerSearchOrder'));
  375.         if Items <> nil then
  376.         begin
  377.           WriteLine('DNS Servers:');
  378.           for Index := 0 to Length(Items) - 1 do
  379.             WriteLine('  ' + Items[Index]);
  380.         end;
  381.       end;
  382.   end;
  383. end;
  384.  
  385. procedure WMIGetSharesInfo(Scope: ManagementScope);
  386. var
  387.   QueryCollection: ManagementObjectCollection;
  388.   Enumerator: ManagementObjectCollection.ManagementObjectEnumerator;
  389.   Item: ManagementObject;
  390. begin
  391.   QueryCollection := GetManagementCollection(Scope, 'Win32_Share');
  392.   Enumerator := QueryCollection.GetEnumerator;
  393.  
  394.   Console.WriteLine;
  395.   Console.WriteLine('Shares Information');
  396.   Console.WriteLine('------------------');
  397.   while Enumerator.MoveNext do
  398.   begin
  399.     Item := Enumerator.Current as ManagementObject;
  400.     with Item, Console do
  401.       WriteLine(GetPropertyValue('Name').ToString + ' - ' +
  402.         GetPropertyValue('Path').ToString);
  403.   end;
  404. end;
  405.  
  406. procedure WMIGetActiveServicesInfo(Scope: ManagementScope);
  407. var
  408.   QueryCollection: ManagementObjectCollection;
  409.   Enumerator: ManagementObjectCollection.ManagementObjectEnumerator;
  410.   Item: ManagementObject;
  411. begin
  412.   QueryCollection := GetManagementCollectionEx(Scope, 'Win32_Service',
  413.     'State=''Running''');
  414.   Enumerator := QueryCollection.GetEnumerator;
  415.  
  416.   Console.WriteLine;
  417.   Console.WriteLine('Active Services Information');
  418.   Console.WriteLine('---------------------------');
  419.   while Enumerator.MoveNext do
  420.   begin
  421.     Item := Enumerator.Current as ManagementObject;
  422.     with Item, Console do
  423.       WriteLine(GetPropertyValue('DisplayName').ToString + ' - ' +
  424.         GetPropertyValue('StartMode').ToString);
  425.   end;
  426. end;
  427.  
  428. procedure WMIGetPrintersInfo(Scope: ManagementScope);
  429. var
  430.   QueryCollection: ManagementObjectCollection;
  431.   Enumerator: ManagementObjectCollection.ManagementObjectEnumerator;
  432.   Item: ManagementObject;
  433. begin
  434.   QueryCollection := GetManagementCollection(Scope, 'Win32_Printer');
  435.   Enumerator := QueryCollection.GetEnumerator;
  436.  
  437.   Console.WriteLine;
  438.   Console.WriteLine('Printers Information');
  439.   Console.WriteLine('--------------------');
  440.   while Enumerator.MoveNext do
  441.   begin
  442.     Item := Enumerator.Current as ManagementObject;
  443.     with Item, Console do
  444.     begin
  445.       Console.Write(GetPropertyValue('DeviceID').ToString + ' - ');
  446.       case uint16.Parse(GetPropertyValue('PrinterStatus').ToString) of
  447.         3: Console.Write('Idle');
  448.         4: Console.Write('Printing');
  449.         5: Console.Write('Warmup');
  450.         6: Console.Write('Stopped Printing');
  451.         7: Console.Write('Offline');
  452.       else
  453.         Console.Write('Unknown');
  454.       end;
  455.       Console.WriteLine;
  456.     end;
  457.   end;
  458. end;
  459.  
  460. procedure WMIGetStartupInfo(Scope: ManagementScope);
  461. var
  462.   QueryCollection: ManagementObjectCollection;
  463.   Enumerator: ManagementObjectCollection.ManagementObjectEnumerator;
  464.   Item: ManagementObject;
  465. begin
  466.   QueryCollection := GetManagementCollection(Scope, 'Win32_StartupCommand');
  467.   Enumerator := QueryCollection.GetEnumerator;
  468.  
  469.   Console.WriteLine;
  470.   Console.WriteLine('Startup Programs Information');
  471.   Console.WriteLine('----------------------------');
  472.   while Enumerator.MoveNext do
  473.   begin
  474.     Item := Enumerator.Current as ManagementObject;
  475.     with Item, Console do
  476.       WriteLine(GetPropertyValue('Name').ToString + ' - ' +
  477.         GetPropertyValue('Command').ToString);
  478.   end;
  479. end;
  480.  
  481. procedure WMIGetEventLogInfo(Scope: ManagementScope);
  482. var
  483.   QueryCollection: ManagementObjectCollection;
  484.   Enumerator: ManagementObjectCollection.ManagementObjectEnumerator;
  485.   Item: ManagementObject;
  486.   Count: Integer;
  487. begin
  488.   QueryCollection := GetManagementCollectionEx(Scope, 'Win32_NTLogEvent',
  489.     'EventType=1');
  490.   Enumerator := QueryCollection.GetEnumerator;
  491.   Count := 0;
  492.  
  493.   Console.WriteLine;
  494.   Console.WriteLine('Application EventLog last 10 errors Information');
  495.   Console.WriteLine('-----------------------------------------------');
  496.   while Enumerator.MoveNext do
  497.   begin
  498.     Item := Enumerator.Current as ManagementObject;
  499.     with Item, Console do
  500.     begin
  501.       Inc(Count);
  502.  
  503.       Console.Write(GetPropertyValue('EventCode').ToString + ' - ' +
  504.         GMTToLocalDateTimeStr(GetPropertyValue('TimeGenerated').ToString));
  505.       if GetPropertyValue('Message') <> nil then
  506.       begin
  507.         Console.Write(' - ');
  508.         if GetPropertyValue('Message').ToString.Length > 40 then
  509.           Console.Write(
  510.             GetPropertyValue('Message').ToString.Substring(0, 40) + '...')
  511.         else
  512.           Console.Write(GetPropertyValue('Message').ToString);
  513.       end;
  514.       Console.WriteLine;
  515.  
  516.       if Count > 10 then
  517.         Break;
  518.     end;
  519.   end;
  520. end;
  521.  
  522. begin
  523.   Console.WriteLine;
  524.   Console.WriteLine('WMI Demo');
  525.   Console.WriteLine('--------');
  526.  
  527.   Scope := System.Management.ManagementScope.Create('\\localhost\root\cimv2');
  528.  
  529.   WMIGetOSInfo(Scope);
  530.   WMIGetComputerInfo(Scope);
  531.   WMIGetBIOSInfo(Scope);
  532.   WMIGetCDROMInfo(Scope);
  533.   WMIGetVideoInfo(Scope);
  534.   WMIGetRegistryInfo(Scope);
  535.   WMIGetDrivesInfo(Scope);
  536.   WMIGetNetConfigInfo(Scope);
  537.   WMIGetSharesInfo(Scope);
  538.   WMIGetPrintersInfo(Scope);
  539.   WMIGetStartupInfo(Scope);
  540.   WMIGetActiveServicesInfo(Scope);
  541.   WMIGetEventLogInfo(Scope);
  542.  
  543.   Console.WriteLine;
  544.   Console.WriteLine('Done. Press <Enter> to exit...');
  545.   Console.WriteLine('------------------------------');
  546.   Console.ReadLine;
  547. end.
  548.